<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>MYWEB</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            /* 设置背景图片 */
            background-image: url('beijing.png');
            /* 替换为你的图片路径 */
            background-size: cover;
            /* 背景图片覆盖整个页面 */
            background-position: center;
            /* 背景图片居中 */
            background-attachment: fixed;
            /* 背景图片固定 */
        }

        .content-wrapper {
            background-color: rgba(255, 255, 255, 0.8);
            /* 半透明白色背景 */
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            width: 300px;
            text-align: center;
        }

        h2 {
            color: #333;
            margin-bottom: 20px;
        }

        h3 {
            color: #555;
            margin-bottom: 10px;
        }

        input[type="text"] {
            width: 120px;
            /* 缩短文本框宽度 */
            padding: 8px;
            margin-bottom: 15px;
            border: 1px solid #ccc;
            border-radius: 5px;
            font-size: 16px;
            text-align: right;
        }

        .radio-group {
            display: flex;
            justify-content: center;
            gap: 10px;
            margin-bottom: 20px;
        }

        .radio-group label {
            font-size: 14px;
            color: #555;
            display: flex;
            align-items: center;
        }

        .radio-group input[type="radio"] {
            transform: scale(1.5);
            margin-right: 5px;
        }

        fieldset {
            border: 1px solid #ccc;
            border-radius: 10px;
            padding: 15px;
            margin-bottom: 20px;
        }

        legend {
            font-size: 16px;
            font-weight: bold;
            color: #333;
            padding: 0 10px;
        }

        input[type="button"] {
            padding: 10px 20px;
            border: none;
            border-radius: 5px;
            background-color: #007bff;
            color: white;
            font-size: 14px;
            cursor: pointer;
        }

        input[type="button"]:hover {
            background-color: #0056b3;
        }
    </style>
    <script>
        function fun(obj) {
            var xhr = new XMLHttpRequest(); // 创建XMLHttpRequest对象
            var url = ""; // 请求的URL，这个可以不写

            if (obj == 'led on') 
            {
                data = "toggle:0,1";

                console.log("req =" + data);               
            }
            else if (obj == 'led off') {
                data = "toggle:0,0";
                console.log("req =" + data);
            }
            else if (obj == 'buzzer on') {
                data = "toggle:1,1";
                console.log("req =" + data);
            }
            else if (obj == 'buzzer off') {
                data = "toggle:1,0";
                console.log("req =" + data);
            }
            xhr.open("POST", url, true); // 配置请求：POST方法、URL、异步请求
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4 && xhr.status === 200) { // 请求完成且成功
                    var response = xhr.responseText; // 获取响应数据
                    console.log("resp = " + response);
                }
            };
            // xhr.send(JSON.stringify(data)); // 发送请求，并将数据转换为JSON字符串
            xhr.send(data);
            console.log(obj);
        }

        function collectData() {
            var xhr = new XMLHttpRequest(); // 创建XMLHttpRequest对象
            var url = ""; // 请求的URL，这个可以不写

            data = "getdata";
            console.log("req = " + data);

            xhr.open("POST", url, true); // 配置请求：POST方法、URL、异步请求

            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4 && xhr.status === 200) { // 请求完成且成功
                    var response = xhr.responseText; // 获取响应数据
                    var parts = response.split(" ");
                    console.log("resp = " + response);
                    document.getElementById("guang").value = parts[0];
                    document.getElementById("x").value = parts[1];
                }
            };

            xhr.send(JSON.stringify(data)); // 发送请求，并将数据转换为JSON字符串
            console.log("数据采集按钮被点击");
        }
    </script>
</head>

<body>
    <div class="content-wrapper">
        <fieldset>
            <legend>信息采集</legend>
            <h3>光照传感器</h3>
            <input type="text" id="guang" placeholder="" readonly>
            <h3>温湿度传感器</h3>
            <input type="text" id="x" placeholder="" readonly>
            <h6></h6>
            <input type="button" value="采集数据" onclick="collectData()">
        </fieldset>

        <fieldset>
            <legend>设备控制</legend>
            <h3>LED灯</h3>
            <div class="radio-group">
                <label>
                    <input type="radio" checked="checked" name="led" value="on" id="led on" onclick="fun(id)"> On
                </label>
                <label>
                    <input type="radio" name="led" value="off" id="led off" onclick="fun(id)"> Off
                </label>
            </div>

            <h3>蜂鸣器</h3>
            <div class="radio-group">
                <label>
                    <input type="radio" name="buzzer" checked="checked" value="on" id="buzzer on" onclick="fun(id)"> On
                </label>
                <label>
                    <input type="radio" name="buzzer" value="off" id="buzzer off" onclick="fun(id)"> Off
                </label>
            </div>
        </fieldset>
    </div>
</body>

</html>